/***************************************************
 ** @Desc : This file for 共有方法
 ** @Time : 2019.01.09 10:38 
 ** @Author : Joker
 ** @File : public_method
 ** @Last Modified by : Joker
 ** @Last Modified time: 2019.01.09 10:38
 ** @Software: GoLand
****************************************************/
package utils

import (
	"sort"
	"bytes"
)

type PublicMethod struct{}

/*将map拼接成字符串,去掉最后一个&符号*/
func (*PublicMethod) ToStringByMapRemoveSign(m map[string]string) (s string) {
	//键排序
	keys := make([]string, 0)
	for k, _ := range m {
		keys = append(keys, k)
	}
	sort.Strings(keys)

	//连接键值对
	var buffer bytes.Buffer
	for i := 0; i < len(m); i++ {
		if m[keys[i]] == "" { //剔除值为空的参数
			continue
		}
		buffer.WriteString(keys[i])
		buffer.WriteString("=")
		buffer.WriteString(m[keys[i]])
		buffer.WriteString("&")
	}
	buf := buffer.String()
	return string([]byte(buf)[:len(buf)-1])
}
